<?php

Yii::import('zii.widgets.grid.CGridView');

class UpdatedGridView  extends CGridView{

    public function init()
    {
        $cookies = Yii::app()->request->getCookies();
        if (!isset($_GET['ajax']) && isset($cookies["$this->id"])) {
            $_GET['ajax'] = $this->id;
            $_GET['sort'] = $cookies["$this->id"]->value;
        } else {
            if (isset($_GET['ajax']) && isset($_GET['sort'])) {

                $cookie = new CHttpCookie($_GET['ajax'], $_GET['sort']);
                $cookie->expire=time()+50000000;
                Yii::app()->request->cookies[$_GET['ajax']] = $cookie;

            }
        }

        parent::init();
    }


    public function getSortInfo($gridview_id, $sidx = 'id')
    {
        $cookies = Yii::app()->request->getCookies();
        $result['sidx'] = $sidx;
        $result['sord'] = '';
        if ((isset($_GET['sort'])) && (isset($_GET['ajax']) && ($_GET['ajax']==$gridview_id))) {
            if (strrpos($_GET['sort'], ".desc")>0) {
                $result['sidx'] = str_replace(".desc", "", $_GET['sort']);
                $result['sord'] = 'DESC';
            } else {
                $result['sidx'] = $_GET['sort'];
            }
        }
        elseif (isset($cookies["$gridview_id"])) {

            if (strrpos($cookies["$gridview_id"]->value, ".desc")>0) {
                $result['sidx'] = str_replace(".desc", "", $cookies["$gridview_id"]->value);
                $result['sord'] = 'DESC';
            } else {
                $result['sidx'] = $cookies["$gridview_id"]->value;
            }
        }
        return $result;
    }

}